package com.mk.admin.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.mk.admin.mapper.MedicalTechnologyProjectMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.MedicalTechnologyProjectDto;
import com.mk.admin.model.dto.MedicalTechnologyProjectRemoteDto;
import com.mk.admin.model.entity.MedicalTechnologyProjectEntity;
import com.mk.admin.model.query.MedicalTechnologyProjectQuery;
import com.mk.admin.model.vo.MedicalTechnologyProjectRemoteVo;
import com.mk.admin.model.vo.MedicalTechnologyProjectVo;
import com.mk.admin.service.MedicalTechnologyProjectService;
import com.mk.admin.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MedicalTechnologyProjectServiceImpl implements MedicalTechnologyProjectService {
    @Autowired
    private MedicalTechnologyProjectMapper medicalTechnologyProjectMapper;

    @Override
    public ResultDto select(MedicalTechnologyProjectQuery query) {
        MedicalTechnologyProjectEntity entity = JSONCopy.copy(query, MedicalTechnologyProjectEntity.class);
        return ResultDto.data(medicalTechnologyProjectMapper.count(entity), medicalTechnologyProjectMapper.select(entity));
    }

    @Override
    public ResultDto add(MedicalTechnologyProjectVo medicalTechnologyProjectVo) {
        MedicalTechnologyProjectEntity entity = JSONCopy.copy(medicalTechnologyProjectVo, MedicalTechnologyProjectEntity.class);
        return ResultDto.data(medicalTechnologyProjectMapper.insert(entity));
    }

    @Override
    public ResultDto update(MedicalTechnologyProjectVo medicalTechnologyProjectVo) {
        MedicalTechnologyProjectEntity entity = JSONCopy.copy(medicalTechnologyProjectVo, MedicalTechnologyProjectEntity.class);
        return ResultDto.data(medicalTechnologyProjectMapper.updateById(entity));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(medicalTechnologyProjectMapper.deleteById(id));
    }

    @Override
    public ResultDto status(int id) {
        return ResultDto.data(medicalTechnologyProjectMapper.status(id));
    }

    @Override
    public ResultDto fresh(MedicalTechnologyProjectRemoteVo vo) {
        String result = "";
        String query = "";
//        if (vo.getMedicaType() != null && !"".equals(vo.getMedicaType())) {
//            JSONObject vvo = new JSONObject();
//            vvo.put("medicaType", vo.getMedicaType());
//            query = JSONObject.toJSONString(vvo);
//            result = insert(query, vo.getMedicaType());
//        } else {
//            JSONObject v1 = new JSONObject();
//            v1.put("medicaType", "05");
//            result = insert(JSONObject.toJSONString(v1), "05");
//
//            JSONObject v2 = new JSONObject();
//            v2.put("medicaType", "06");
//            result += insert(JSONObject.toJSONString(v2), "06");
//        }
        return ResultDto.data("success", "获得 " + result);
    }

    private String insert(String query, String type) {
//        JSONObject jsonObject = HttpTool.postJson("com_0009", query);
//        Object result = jsonObject.get("result");
//        List<MedicalTechnologyProjectRemoteDto> list = new ArrayList<>();
//        if (result != null && !"[]".equals(result)) {
//            list = JSONArray.parseArray(result.toString(), MedicalTechnologyProjectRemoteDto.class);
//            list.forEach(item -> {
//                MedicalTechnologyProjectEntity entity = new MedicalTechnologyProjectEntity();
//                entity.setCode(item.getSetMealCode());
//                List<MedicalTechnologyProjectDto> select = medicalTechnologyProjectMapper.select(entity);
//                entity.setMoney(NumberTool.strToMoney(item.getSetMealMoney()));
//                entity.setName(item.getSetMealName());
//                entity.setMedicalType(type);
//                entity.setStatus(1);
//                entity.setDoctorCode(item.getBillDoctorCode());
//                entity.setDoctorName(item.getBillDoctorName());
//                entity.setDepartmentCode(item.getExecDeptCode());
//                entity.setDepartmentName(item.getExecDeptName());
//                if ("05".equals(type)) {
//                    entity.setMedicalTypeName("检验");
//                }
//                if ("06".equals(type)) {
//                    entity.setMedicalTypeName("检查");
//                }
//                if (!StringTool.isBlank(item.getSetMealDes())) {
//                    entity.setDescribe(item.getSetMealDes());
//                }
//                if (select.size() == 0) {
//                    entity.setCreateTime(DateTool.format());
//                    medicalTechnologyProjectMapper.insert(entity);
//                } else {
//                    entity.setId(select.get(0).getId());
//                    medicalTechnologyProjectMapper.updateById(entity);
//                }
//            });
//        }
//        return list.size() + "条 " + ("05".equals(type) ? "检验 " : "检查 ");
        return "";
    }
}
